package com.liu.usky.rlzp.uskyrlzp.util;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;

public class DateUtils {
	
	public static Long getTime(LocalDateTime dataeTime) {
		Instant instant = dataeTime.toInstant(ZoneOffset.UTC);
		return Date.from(instant).getTime();
	}
	public static LocalDateTime getTomorrowEndOfDay() {
		LocalDateTime nowTime = LocalDateTime.now().with(LocalTime.MAX);
		return nowTime.plusDays(1L);
	}
	public static Date getTomorrowEndOfDayDate() {
		LocalDateTime nowTime = LocalDateTime.now().with(LocalTime.MAX);
		Instant instant = nowTime.plusDays(1L).toInstant(ZoneOffset.UTC);
		return Date.from(instant);
	}
	
	public static LocalDateTime getTomorrowStartOfDay() {
		LocalDateTime nowTime = LocalDateTime.now().with(LocalTime.MIDNIGHT);
		return nowTime.plusDays(1L);
	}
	
	public static boolean IsDeadTime(LocalDateTime nowTime,LocalDateTime targetTime) {
		return !nowTime.isBefore(targetTime);
	}
	
	public static Clock getClock() {
		//返回当前时间，根据系统时间和UTC
		Clock clock = Clock.systemUTC();
		return clock;
	}
	
	/**
	 * 
	 * @param dateTime
	 * @param pattern yyyy年MM月dd日 HH:mm:ss
	 * @return
	 */
	public static String formatDateTimeToStr(LocalDateTime dateTime,String pattern) {
		DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(pattern);
		return formatter2.format(dateTime);
	}
	/**
	 * 
	 * @param date 
	 * @param pattern yyyy-MM-dd
	 * @return
	 */
	public static String formatDateToStr(LocalDate date,String pattern) {
		DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(pattern);
		return formatter2.format(date);
	}
	/**
	 * 
	 * @param time 
	 * @param pattern HH:mm:ss
	 * @return
	 */
	public static String formaTimeToStr(LocalTime time,String pattern) {
		DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(pattern);
		return formatter2.format(time);
	}
	
	/**
	 * 
	 * @param dateTimeStr
	 * @param pattern yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static LocalDateTime strToDateTime(String dateTimeStr,String pattern) {
		DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern(pattern);
		LocalDateTime ldt = LocalDateTime.parse(dateTimeStr, formatter3); 
		return ldt;
	}
	
	public static LocalDate getNowDate() {
		LocalDate nowDate = LocalDate.now();
		return nowDate;
	}
	
	public static LocalTime getNowTime() {
		LocalTime localTime = LocalTime.now();
		return localTime;
	}
	
	public static LocalDate makeDate(Integer year,Integer month,Integer dayOfMonth) {
		return LocalDate.of(year, month, dayOfMonth);
	}
	
	public static LocalTime makeTime(Integer hour,Integer minute,Integer second) {
		return LocalTime.of(hour, minute, second);
	}
	
	public static LocalTime makeTime(Integer hour,Integer minute) {
		return LocalTime.of(hour, minute);
	}
	
	public static LocalDateTime makeDateTime(Integer year,Integer month,Integer dayOfMonth,Integer hour,Integer minute,Integer second) {
		return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
	}
	
	public static LocalDateTime makeDateTime(Integer year,Integer month,Integer dayOfMonth,Integer hour,Integer minute) {
		return LocalDateTime.of(year, month, dayOfMonth, hour, minute);
	}
	
}
